/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.core.base.validation.config;

import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 参数校验配置
 *
 * @author 挺好的 2023年05月30日 9:16
 */
@Configuration
public class ValidatorConfig {

    /**
     * 校验模式，快速失败
     */
    private static final String VALIDATOR_FAIL_FAST_PROPERTY = "hibernate.validator.fail_fast";

    private static final String TRUE_ATTRIBUTE_VALUE = "true";

    /**
     * 配置校验器，使用快速失败模式
     *
     * @return
     */
    @Bean
    public Validator validator () {
        
        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure()
                .addProperty(VALIDATOR_FAIL_FAST_PROPERTY, TRUE_ATTRIBUTE_VALUE).buildValidatorFactory();

        return validatorFactory.getValidator();
    }
}
